/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 * @description 中序遍历 迭代
 */

var inorderTraversal = function (root) {
    //  初始化结果数组
    let res = []
    // 初始化栈结构
    let stack = []
    let cur = root
    while (cur || stack.length) {
        // 寻找最左边的节点，并一次将路过节点放入stack
        while (cur) {
            stack.push(cur)
            cur = cur.left
        }
        // 取地栈顶元素
        cur = stack.pop()
        // 推入结果数组
        res.push(cur.val)
        // 查看是否有右节点
        cur = cur.right
    }
    return res
};